/**
 * 
 */
package com.fox.webapp.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fox.webapp.domain.Account;
import com.fox.webapp.domain.Broker;
import com.fox.webapp.domain.CommissionRecord;
import com.fox.webapp.domain.Person;
import com.fox.webapp.service.IAccountService;
import com.fox.webapp.service.IBrokerService;
import com.fox.webapp.utils.Constant;

/**
 * @author yockii
 *
 */
@Controller
@RequestMapping("account")
public class AccountController {
	private static final Logger logger = LoggerFactory.getLogger(AccountController.class);
	
	@Autowired
	private IBrokerService brokerService;
	@Autowired
	private IAccountService accountService;
	
	@RequestMapping(value = {"", "index", "index.html"}, method = RequestMethod.GET)
	public String myAccount(HttpSession session, Model model){
		Person p = (Person)session.getAttribute(Constant.SESSION_USER);//personService.getPersonById(((Person)session.getAttribute(Constant.SESSION_USER)).getId());
		model.addAttribute("accountList", accountService.getMyAccounts(p));
		List<Broker> bs = brokerService.getAllActivedBroker();
		model.addAttribute("brokerList", bs);
		return "account/main";
	}
	
	@RequestMapping(value={"addNewAccount"}, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addNewAccount(Account account, String brokerId, HttpSession session){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put(Constant.RESULT_STR, Constant.RESULT_FAILED);
		Person person = (Person)session.getAttribute(Constant.SESSION_USER);
		try {
			Account a = accountService.addNewAccount(account, brokerId, person);
			if(a != null && a.getId() != null){
				resultMap.put(Constant.RESULT_STR, Constant.RESULT_SUCCESS);
				resultMap.put("accountId", a.getId().toString());
			} else {
				logger.error("用户:"+person.getUsername() + "     " + brokerId + "添加新账户出现重复帐号" + account.getAccountNo());
			}
		} catch (Exception e) {
			logger.error("用户:"+person.getUsername() + "     " + brokerId + "添加新账户出现异常" + account.getAccountNo());
		}
		return resultMap;
	}
	
	@RequestMapping(value={"removeAccount"}, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Integer> removeAccounts(String accountId, HttpSession session){
		Map<String, Integer> resultMap = new HashMap<String, Integer>();
		resultMap.put(Constant.RESULT_STR, Constant.RESULT_FAILED);
		Person person = (Person)session.getAttribute(Constant.SESSION_USER);
		try {
			boolean b = accountService.removeMyAccount(person, accountId);
			if(b){
				resultMap.put(Constant.RESULT_STR, Constant.RESULT_SUCCESS);
			}
		} catch (Exception e) {
			logger.error("用户:"+person.getUsername()  + "删除账户出现异常" + accountId);
		}
		
		
		return resultMap;
	}
	
	@RequestMapping(value={"modifyAccountName"}, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Integer> UpdateAccountName(Account account, HttpSession session){
		Map<String, Integer> resultMap = new HashMap<String, Integer>();
		resultMap.put(Constant.RESULT_STR, Constant.RESULT_FAILED);
		Person person = (Person)session.getAttribute(Constant.SESSION_USER);
		try {
			boolean b = accountService.updateAccountName(person, account);
			if(b)
				resultMap.put(Constant.RESULT_STR, Constant.RESULT_SUCCESS);
		} catch (Exception e) {
			logger.error("用户:"+person.getUsername()  + "修改账户名称出现异常" + account.getSelfDesc());
		}
		
		
		return resultMap;
	}
	
	@RequestMapping(value = "comm/{id}/{page}", method = RequestMethod.GET)
	public String showAccountCommission(@PathVariable ObjectId id, @PathVariable int page, Model model){
		Account account = accountService.getAccount(id);
		model.addAttribute("account", account);
		if(account != null && account.getActive() != null && account.getActive().booleanValue()){
			Page<CommissionRecord> crPage = accountService.getMyCommission(account, page, 15);
			model.addAttribute("crPage", crPage);
		} else {
			model.addAttribute("crPage", new PageImpl<CommissionRecord>(new ArrayList<CommissionRecord>()));
		}
		return "account/commission";
	}

}
